/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Entity.Veterinarian;
import Services.VeterinarianService;
import Services.exceptions.NonexistentEntityException;
import java.util.Calendar;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.SpinnerDateModel;

/**
 *
 * @author arch
 */
public class DVeterinarian extends javax.swing.JDialog {

  /**
   * Creates new form DVeterinarian
   */
    private EntityManagerFactory emf=null;
  public DVeterinarian(java.awt.Frame parent, boolean modal) {
    super(parent, modal);
    initComponents();
    
    Calendar cal = Calendar.getInstance();
    
    cal.set(Calendar.YEAR, 1900);
    cal.set(Calendar.MONTH, 1);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    
    Date start = cal.getTime();
    
    cal.set(Calendar.YEAR, 2050);
    cal.set(Calendar.MONTH, 1);
    cal.set(Calendar.DAY_OF_MONTH, 1);
    
    Date end = cal.getTime();
    
    spinnerModel = new SpinnerDateModel(start, start, end, Calendar.YEAR);
    spinnerDate.setModel(spinnerModel);
    emf=Persistence.createEntityManagerFactory("HPVASPU");
    super.setDefaultCloseOperation(JDialog.HIDE_ON_CLOSE);
    super.pack();
    super.setVisible(false);
    super.setLocationRelativeTo(this);
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    spinnerDate = new javax.swing.JSpinner();
    lFocusOfPractice = new javax.swing.JLabel();
    lPets = new javax.swing.JLabel();
    tfSSN = new javax.swing.JTextField();
    tfFocusOfPractice = new javax.swing.JTextField();
    lName = new javax.swing.JLabel();
    lSpecialities = new javax.swing.JLabel();
    jLabel1 = new javax.swing.JLabel();
    lLastname = new javax.swing.JLabel();
    bClose = new javax.swing.JButton();
    lSalary = new javax.swing.JLabel();
    lBirthdate = new javax.swing.JLabel();
    tfSpecialities = new javax.swing.JTextField();
    tfName = new javax.swing.JTextField();
    tfLastname = new javax.swing.JTextField();
    tfGraduateAt = new javax.swing.JTextField();
    tfSalary = new javax.swing.JTextField();
    bDelete = new javax.swing.JButton();
    lGraduateAt = new javax.swing.JLabel();
    scrollPane = new javax.swing.JScrollPane();
    pPets = new javax.swing.JPanel();
    bSave = new javax.swing.JButton();

    setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

    lFocusOfPractice.setText("Focus of Practice:");

    lPets.setText("Pets:");

    lName.setText("Name:");

    lSpecialities.setText("Specialities:");

    jLabel1.setText("SSN:");

    lLastname.setText("Lastname:");

    bClose.setText("Close");
    bClose.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bCloseActionPerformed(evt);
      }
    });

    lSalary.setText("Salary:");

    lBirthdate.setText("Birth-date:");

    bDelete.setText("Delete");
    bDelete.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bDeleteActionPerformed(evt);
      }
    });

    lGraduateAt.setText("Graduate At:");

    pPets.setLayout(new java.awt.GridLayout());
    scrollPane.setViewportView(pPets);

    bSave.setText("Save");
    bSave.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        bSaveActionPerformed(evt);
      }
    });

    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addComponent(scrollPane)
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(lName)
              .addComponent(jLabel1))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
              .addComponent(tfName)
              .addComponent(tfSSN, javax.swing.GroupLayout.DEFAULT_SIZE, 190, Short.MAX_VALUE))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(0, 0, Short.MAX_VALUE)
                .addComponent(bDelete)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(bSave)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(bClose))
              .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addComponent(lLastname)
                  .addComponent(lBirthdate, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                  .addComponent(tfLastname)
                  .addGroup(layout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(spinnerDate, javax.swing.GroupLayout.PREFERRED_SIZE, 227, javax.swing.GroupLayout.PREFERRED_SIZE))))))
          .addGroup(layout.createSequentialGroup()
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
              .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                .addComponent(lGraduateAt)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(tfGraduateAt))
              .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                  .addComponent(lFocusOfPractice)
                  .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                  .addComponent(tfFocusOfPractice, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addComponent(lPets)))
            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(lSpecialities)
              .addComponent(lSalary))
            .addGap(16, 16, 16)
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addComponent(tfSpecialities, javax.swing.GroupLayout.DEFAULT_SIZE, 162, Short.MAX_VALUE)
              .addComponent(tfSalary))))
        .addContainerGap())
    );
    layout.setVerticalGroup(
      layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
      .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
        .addContainerGap()
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(lName)
          .addComponent(lLastname)
          .addComponent(tfName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(tfLastname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(jLabel1)
          .addComponent(tfSSN, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(lBirthdate)
          .addComponent(spinnerDate, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(lFocusOfPractice)
          .addComponent(tfFocusOfPractice, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
          .addComponent(lSpecialities)
          .addComponent(tfSpecialities, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
          .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
            .addComponent(lGraduateAt)
            .addComponent(tfGraduateAt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
          .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
            .addComponent(lSalary)
            .addComponent(tfSalary, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
        .addComponent(lPets)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addComponent(scrollPane, javax.swing.GroupLayout.PREFERRED_SIZE, 296, javax.swing.GroupLayout.PREFERRED_SIZE)
        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
          .addComponent(bClose)
          .addComponent(bSave)
          .addComponent(bDelete))
        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
    );

    pack();
  }// </editor-fold>//GEN-END:initComponents

  private void bCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bCloseActionPerformed
    
    setVisible(false);
  }//GEN-LAST:event_bCloseActionPerformed

  private void bDeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bDeleteActionPerformed
    
    if (!saved) {
      JOptionPane.showMessageDialog(this, "Veterinarian not saved yet", "Info", JOptionPane.INFORMATION_MESSAGE);
      return;
    }
    int op = DeleteCancel("Are You Sure?", "Delete");
    if (op == JOptionPane.NO_OPTION) return;
    
      // llamar al DAO aqui'
    
    VeterinarianService vs=new VeterinarianService(emf);
        try {
            vs.destroy(vet.getId());
        } catch (NonexistentEntityException ex) {
            Logger.getLogger(DVeterinarian.class.getName()).log(Level.SEVERE, null, ex);
            JOptionPane.showMessageDialog(this,"The id of Veterinarian don't find in database","Error",ERROR);
        }
    setVisible(false);
  }//GEN-LAST:event_bDeleteActionPerformed

  private void bSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bSaveActionPerformed
    
    boolean ok = true;
    
    // Chekear valores en los campos aqui'
    // asigne el resultado a ok
    
    if (!ok) {
      JOptionPane.showMessageDialog(this, "Invalid data", "Error", JOptionPane.ERROR_MESSAGE);
      return;
    }
    saved = true;
      int salary = Integer.parseInt(tfSalary.getText());
      vet.setFocus_of_practice(this.tfFocusOfPractice.getText());
      vet.setGraduateAt(this.tfGraduateAt.getText());
      vet.setName(tfName.getText());
      vet.setLastname(tfLastname.getText());
      vet.setSSN(tfSSN.getText());
      vet.setSalary(salary);
      vet.setSpecialities(tfSpecialities.getText());
      vet.setBirthdate(spinnerModel.getDate());
    // llamar al DAO aqui'
    VeterinarianService vs= new VeterinarianService(emf);
    vs.create(vet);
    clear();
      
    
  }//GEN-LAST:event_bSaveActionPerformed

  public void set(Veterinarian v) {

    if (v == null) {
      clear();
      saved = false;
      vet = new Veterinarian();
      setTitle("New Veterinarian");
    } else {
      vet = v;

      saved = true;
      setTitle(v.getName());
      tfName.setText(v.getName());
      tfLastname.setText(v.getLastname());
      tfSSN.setText(v.getSSN());
      tfFocusOfPractice.setText(v.getFocus_of_practice());
      tfGraduateAt.setText(v.getGraduateAt());
      tfSalary.setText(v.getSalary() + "");
      tfSpecialities.setText(v.getSpecialities());

      spinnerModel.setValue(spinnerModel.getStart());
    }
  }

  public Veterinarian get() {
    return vet;
  }

  public void clear() {
    
    tfName.setText("");
    tfLastname.setText("");
    tfSSN.setText("");
    tfFocusOfPractice.setText("");
    tfGraduateAt.setText("");
    tfSalary.setText("");
    tfSpecialities.setText("");
    
    spinnerModel.setValue(spinnerModel.getStart());
  }
  
  public int DeleteCancel(String message, String title) {
    
    Object[] options = {"Yes", "Cancel"};
    int op = JOptionPane.showOptionDialog(this, message, title,
                                          JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE,
                                          null, options, JOptionPane.YES_OPTION);
    return op;
  }
  
  boolean saved;
  SpinnerDateModel spinnerModel;
  Veterinarian vet;
  
  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JButton bClose;
  private javax.swing.JButton bDelete;
  private javax.swing.JButton bSave;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel lBirthdate;
  private javax.swing.JLabel lFocusOfPractice;
  private javax.swing.JLabel lGraduateAt;
  private javax.swing.JLabel lLastname;
  private javax.swing.JLabel lName;
  private javax.swing.JLabel lPets;
  private javax.swing.JLabel lSalary;
  private javax.swing.JLabel lSpecialities;
  private javax.swing.JPanel pPets;
  private javax.swing.JScrollPane scrollPane;
  private javax.swing.JSpinner spinnerDate;
  private javax.swing.JTextField tfFocusOfPractice;
  private javax.swing.JTextField tfGraduateAt;
  private javax.swing.JTextField tfLastname;
  private javax.swing.JTextField tfName;
  private javax.swing.JTextField tfSSN;
  private javax.swing.JTextField tfSalary;
  private javax.swing.JTextField tfSpecialities;
  // End of variables declaration//GEN-END:variables
}
